﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace One_day.Areas.admin.Controllers
{
    public class Area_UserController : Controller
    {
        // GET: admin/User
        public ActionResult Index()
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            List<One_day.Models.T_Base_User> user = db.T_Base_User.Where(m => m.Type == 2).ToList();
            ViewBag.user = user;
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }
        public ActionResult AddSave(String LoginName,String RealName,String PWD)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Base_User user = new Models.T_Base_User();
            user.LoginName = LoginName;
            user.RealName = RealName;
            user.PWD = PWD;
            user.Type = 2;
            db.T_Base_User.Add(user);
            db.SaveChanges();
            return Redirect("/admin/area_user/index");
        }

        public JsonResult Delete(int id)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Base_User user = db.T_Base_User.Single(m => m.Id == id);
            int result;
            try
            {
                db.T_Base_User.Remove(user);
                result = db.SaveChanges();
            }
            catch
            {
                return Json(new { code = 200, message = "账号正在被使用，无法删除" });
            }
            if (result > 0)
                return Json(new { code = 100, message = "删除成功" });
            else
                return Json(new { code=300, message="出现未知错误" });
        }
    }
}